<?php
	require_once('classes/CustomFields.class.php');
		
	$field_id = dpGetParam( $_REQUEST, 'field_id', 0 );
	$delete_field = dpGetParam( $_GET, 'delete', 0 );
	$module = dpGetParam($_REQUEST, 'module', NULL );
	$select_items = dpGetParam($_POST, 'select_items', Array());

	// Loading the page for the first time
	if (dpGetParam( $_REQUEST, 'field_id', NULL) != NULL) {
		$custom_fields = new CustomFields($module, 'addedit', NULL, 'edit');

		if ($delete_field) {
			$custom_fields->deleteField( $field_id );
			$AppUI->setMsg('Selected field has been deleted.', UI_MSG_INFO);
			echo $AppUI->getMsg();
			exit();
		}

		$cf =& $custom_fields->fieldWithId( $field_id );

		if (is_object($cf)) {
			$field_name = $cf->fieldName();
			$field_description = $cf->fieldDescription();
			$field_htmltype = $cf->fieldHtmlType();
			$field_extratags = $cf->fieldExtraTags();

			if ($field_htmltype == 'select') {
				$select_options = new CustomOptionList( $field_id );
				$select_options->load();
				$select_items = $select_options->getOptions();
			}
		}
		else {
			$AppUI->setMsg('Couldnt load the Custom Field,<br/>It might have been deleted somehow.', UI_MSG_ERROR);
			echo $AppUI->getMsg();
			exit();
		}

		$edit_title = $AppUI->_('Edit Custom Field In'); 
	}
	else {
		$edit_title = $AppUI->_('New Custom Field In');

		$field_name = dpGetParam( $_POST, 'field_name', NULL );
		$field_description = dpGetParam( $_POST, 'field_description', NULL );
		$field_htmltype = dpGetParam( $_POST, 'field_htmltype', 'textinput');
		$field_extratags = dpGetParam( $_POST, 'field_extratags', NULL );
	}

	$html_types = Array(
		'textinput'	=> $AppUI->_('Text Input'),
		'textarea'	=> $AppUI->_('Text Area'),
		'checkbox'	=> $AppUI->_('Checkbox'),
		'select'	=> $AppUI->_('Select List'),
		'label'		=> $AppUI->_('Label'),
		'separator'	=> $AppUI->_('Separator'),
		'href'		=> $AppUI->_('Weblink'),
	);
?>
<form method="POST" action="?m=system&a=custom_field_editor" name="custform" />
<table border="0" cellspacing="0" cellpadding="4">
<th colspan="4" align="center" bgcolor="#E0E0E0">
	<?=$edit_title?> <?=$AppUI->_($module)?> <?=$AppUI->_('Module') ?>
	<input type="hidden" name="field_id" value="<?=$field_id?>" />
	<input type="hidden" name="module" value="<?=$module?>" /> 
	<input type="hidden" name="dontdosql" id="dosql" value="do_custom_field_aed" />
</td></tr>
<tr>
	<td rowspan="5">&nbsp;</td>
	<td align="right"><?=$AppUI->_('Field Name/Identifier')?>:</td>
	<td><input type="text" name="field_name" maxlength="100" value="<?=$field_name?>"
			onblur="this.value=this.value.replace(/[^a-z|^A-Z|^0-9]*/gi,'')"
			style="width: 200px"/></td>
	<td rowspan="5">&nbsp;</td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Field Description')?>:</td>
	<td><input type="text" name="field_description" maxlength="250" value="<?=$field_description?>"
			style="width: 200px"/></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Field Display Type')?>:</td>
	<td><?=arraySelect( $html_types, 'field_htmltype',
				'style="width: 205px" onChange="doCheckFieldType(this)"', $field_htmltype)?></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('HTML Tag Options')?>:</td>
	<td><input type="text" name="field_extratags" value="<?=$field_extratags?>"
			style="width: 200px"/></td>
</tr>
<tr id="idSelectAttributes" <?=$field_htmltype != 'select' ? 'style="visibility: hidden; display: none"' : ''?>>
	<td align="right" valign="top" style="padding-top: 8px"><?=$AppUI->_('List of Options')?>:</td>
	<td valign="top">
		<div class="text">
		<input id="idNewItem" type="text" class="spin" style="width: 180px; text-align: left"/>
		<input type="image" class="ico" src="images/common_add.gif" onClick="doAddItem(); return false"/>
		</div>
		<div style="width: 100%; height: 60px; overflow: auto; margin-top: 2px; border: 1px solid #E0E0E0">
		<table id="idTableItem" width="100%" cellpadding="2" cellspacing="0" border="0">
		<? foreach( $select_items as $itm) { ?>
		<tr>
			<td>
				<img src="images/common_delete.gif" class="clickable" onclick="doDeleteItem(this)"/>&nbsp;<?=$itm?><input type="hidden" name="select_items[]" value="<?=$itm?>"/>
			</td>
		</tr>
		<? } ?>
		</table>
		</div>
	</td>
</tr>
<tr>
	<td colspan="4" align="center" style="border-top: 1px solid #E0E0E0; padding: 6px">
		<input type="button" value="Save" onClick="doSaveCustomField()" />
		<input type="button" value="Close" onClick="doCloseDialog()" />
	</td>
</tr>
</table>
</form>
